#include "func_key.h"
#include <stdio.h>
#include "common/framework/platform_init.h"
#include "common/apps/buttons/buttons.h"
#include "common/apps/buttons/buttons_low_level.h"

#define KEY_RECORD      KEY1
#define KEY_UPLOAD      KEY2
#define KEY_DELETE      KEY3
#define KEY_DIMENSION   KEY4
#define KEY_POWER       KEY6


static button_handle *button_record = NULL;
static button_handle *button_upload = NULL;
static button_handle *button_delete = NULL;
static button_handle *button_dimension = NULL;
static button_handle *button_power = NULL;

/**
 * @brief record key callback 
 * 
 * @param sta 
 * @param arg 
 */
static void button_record_cb(BUTTON_STATE sta, void *arg){
    printf("[%s] cb \n",__FUNCTION__);
}

/**
 * @brief upload key callback
 * 
 * @param sta 
 * @param arg 
 */
static void button_upload_cb(BUTTON_STATE sta, void *arg){
    printf("[%s] cb \n",__FUNCTION__);

}

/**
 * @brief delete key callback
 * 
 * @param sta 
 * @param arg 
 */
static void button_delete_cb(BUTTON_STATE sta, void *arg){
    printf("[%s] cb \n",__FUNCTION__);

}

/**
 * @brief dimension key callback
 * 
 * @param sta 
 * @param arg 
 */
static void button_dimension_cb(BUTTON_STATE sta, void *arg){
    printf("[%s] cb \n",__FUNCTION__);

}

/**
 * @brief power kye callback
 * 
 * @param sta 
 * @param arg 
 */
static void button_power_cb(BUTTON_STATE sta, void *arg){
    
    if (sta == PRESS){
        printf("[%s] cb - press \n",__FUNCTION__);
    }else if (sta == RELEASE){
        printf("[%s] cb - release \n",__FUNCTION__);
    }
}


void func_key_init(){
    if (button_record)
    {
        printf("key already init \n");
        return ;
    }
    
	/* register the low level button interface */
	button_impl_t impl = {
		buttons_low_level_init,
		buttons_low_level_get_state,
		buttons_low_level_wait_semaphore,
		buttons_low_level_release_semaphore,
		buttons_low_level_deinit
	};
	/* set buttons thread priority and stack size */
	buttons_set_thread_priority(4);
	buttons_set_thread_stack_size(1024);

	/* init buttons, will init the low level buttons, ad buttons and gpio buttons */
	buttons_init(&impl);

    button_record = create_short_button(KEY_RECORD);
    button_upload = create_short_button(KEY_UPLOAD);
    button_delete = create_short_button(KEY_DELETE);
    button_dimension = create_short_button(KEY_DIMENSION);
    button_power = create_long_button(KEY_POWER, 1000);

    button_record->cb = button_record_cb ;
    button_upload->cb = button_upload_cb ;
    button_delete->cb = button_delete_cb ;
    button_dimension->cb = button_dimension_cb ;
    button_power->cb = button_power_cb ;

    button_record->start(button_record);
    button_upload->start(button_upload);
    button_delete->start(button_delete);
    button_dimension->start(button_dimension);
    button_power->start(button_power);
}

void func_key_uninit(){
    if (button_record){
        button_record->stop(button_record);
        button_record->destroy(button_record);
        button_record = NULL ;
    }
    if (button_upload){
        button_upload->stop(button_upload);
        button_upload->destroy(button_upload);
        button_upload = NULL ;
    }
    if (button_delete){
        button_delete->stop(button_delete);
        button_delete->destroy(button_delete);
        button_delete = NULL ;
    }
    if (button_dimension){
        button_dimension->stop(button_dimension);
        button_dimension->destroy(button_dimension);
        button_dimension = NULL ;
    }
    if (button_power){
        button_power->stop(button_power);
        button_power->destroy(button_power);
        button_power = NULL ;
    }     
    buttons_deinit();       
}